global class ContractSyncUtil {

  /*
  Webservice static String postOA(String conid ){


        Map<String,String> mainMap = new Map<String,String>();
        //1.获取人员的OA id 
        Contract__c  con = [select RecordType.DeveloperName ,TerminalCustomer__r.Name ,Account__r.DealerType__c ,Account__r.RecordType.DeveloperName , Status__c,ProcessID__c, ContractURL__c,isSycToOa__c,Id,Account__r.Name ,Maccura__c, Maccura__r.AccountNumber,CustomerContact__r.Name , CustomerPhone__c  from Contract__c where Id =:conid];

        if(con == null)return '合同错误!不能发起OA流程!';


        if(con.Status__c == '已生效'|| con.Status__c == '已终止') return '合同'+con.Status__c +'!不能发起OA流程';
       
        if(con.isSycToOa__c == true) return '已同步OA不要重复同步!';

        if(con.Account__r.RecordType.DeveloperName == 'Dealer' && con.Account__r.DealerType__c == '') return '对方主体经销商类型不能为空!';
        User u = [select Id, EmployeeNumber from User  where Id =: UserInfo.getUserId()];
        if(u.EmployeeNumber == null) return '请维护合同创建人的工号!'; 

        String emNumber = null ;

        try {
           emNumber =  u.EmployeeNumber.replaceFirst('^0+', '');
        }catch (Exception e) {
            return '解析发起审批人工号错误:'+e.getMessage();
        }
        //if(con.Maccura__c == null) return '我方主体必填!';
        //if(con.Maccura__r.AccountNumber == null || con.Maccura__r.AccountNumber =='' )return '请维护我方主体的客户编码!';

        HttpRequest request = new HttpRequest();
        request.setMethod('GET');
        request.setEndpoint(Label.GetOAPersonCode+'?userID='+emNumber);
        HttpResponse res=  Utils.postRequest(request,1);

         if (res.getStatusCode() == 200) {
                Map<String,Object> r = new Map<String,Object>();
                //如果是测试
                if(Test.isRunningTest()){

                    Map<String,Object>  resBody= new Map<String,Object>();
                  resBody.put('code','0');
                  resBody.put('result','<UserBean-array>  <UserBean>    <userid>4854</userid>    <subcompanyid1>126</subcompanyid1>    <departmentid>3009</departmentid>    <subcompanyname>迈克电子</subcompanyname>    <departmentname>四川</departmentname>    <workcode>2896</workcode>    <lastname>文明红</lastname>    <loginid>2896</loginid>    <password>E547BF53F93AD2A0027F8F54FE28EE95</password>    <seclevel>20</seclevel>    <sex>女</sex>    <jobtitle>449</jobtitle>    <jobcall></jobcall>    <joblevel>17</joblevel>    <jobactivitydesc></jobactivitydesc>    <managerid>4848</managerid>    <assistantid></assistantid>    <status>1</status>    <locationid>21</locationid>    <workroom></workroom>    <telephone>81694</telephone>    <mobile>15882203634</mobile>    <mobilecall></mobilecall>    <fax></fax>    <email>wenminghong@maccura.com</email>    <systemlanguage></systemlanguage>    <birthday></birthday>    <folk></folk>    <nativeplace></nativeplace>    <regresidentplace></regresidentplace>    <certificatenum></certificatenum>    <maritalstatus>未婚</maritalstatus>    <policy></policy>    <bememberdate></bememberdate>    <bepartydate></bepartydate>    <islabouunion></islabouunion>    <educationlevel></educationlevel>    <degree></degree>    <healthinfo></healthinfo>    <height></height>    <weight></weight>    <residentplace></residentplace>    <homeaddress></homeaddress>    <tempresidentnumber></tempresidentnumber>    <startdate>2016-12-05</startdate>    <enddate>2900-01-01</enddate>    <createdate>2018-07-13</createdate>    <lastChangdate>2018-08-03</lastChangdate>    <accounttype>0</accounttype>    <dsporder>4983.0</dsporder>  </UserBean></UserBean-array>');
                    r =  resBody;
                }else{
                     r  = (Map<String,Object> ) JSON.deserializeUntyped(res.getBody());
                }
                if(r.get('code') == 0 || r.get('code') == '0'){
                
                   if ( r.get('result') != null ) {
                        String jsr = (String)r.get('result');
                          //格式化
                        Pattern p = Pattern.compile('\\s*|\t|\r|\n');
                        Matcher m_jsr = p.matcher(jsr);
                        String n_jsr = m_jsr.replaceAll('');
                        //String xml_o = n_jsr.replace('<UserBean-array>', '');
                        //String n_xml = xml_o.replace('</UserBean-array>', '');
                       // System.debug(n_xml);
                        //XML解析
                        Dom.Document doc = new Dom.Document(); 
                        doc.load(n_jsr);

                        Dom.XMLNode userDataInfo = doc.getRootElement();

                        String userid = '';
                        String subcompanyid1 = '';
                        String departmentid = '';
                        

      //                   0：试用
      // 1：正式
      // 2：临时
      // 3：试用延期
      // 4：解聘
      // 5：离职
      // 6：退休
      // 7：无效
                        

                        for(Dom.XMLNode node : userDataInfo.getChildren()){

                            if(Integer.valueOf(node.getChildElement('status',null ).getText() ) < 4 ){

                              userid = node.getChildElement('userid', null).getText(); 
                              subcompanyid1= node.getChildElement('subcompanyid1', null).getText();
                              departmentid = node.getChildElement('departmentid', null).getText();
                              break;
                            }
                        }



                        mainMap.put('sqr',userid);//申请人;
                        mainMap.put('ssbm',departmentid);//所属部门;
                        mainMap.put('ssgs',subcompanyid1);//所属公司;

                        mainMap.put('htsplx','0');//合同类型
                        mainMap.put('ej','0');//二级分类
                        mainMap.put('zxfx','0');//直销/分销
                        mainMap.put('sfxm','0');//是否项目
                        mainMap.put('sfwdbyw','0');//是否为代开业务
                        mainMap.put('htqx','0');//合同期限
                        mainMap.put('bz','0');//币种
                        mainMap.put('hthtje','0');//合同金额
                        mainMap.put('htly','0');//合同来源
                        mainMap.put('fkfx','0');//付款方向
                        mainMap.put('qdlx','0');//签订类型
                        mainMap.put('sfbh',conid);//合同编号
                        String title = '与'+con.Account__r.Name +'-'+con.TerminalCustomer__r.Name + '的销售合同审批';
                        String level = '0';
                        String creatorId = userid;

                        String workflowId = Label.OAContractFlowID;//''3083'';
                      
                        Map<String,String> detailMap = new Map<String,String>();
                        detailMap.put('htxdf', con.Account__r.Name );//合同相对方
                        detailMap.put('xdflxr', con.CustomerContact__r.Name );//合同相对方联系人
                        detailMap.put('xdflxfs', con.CustomerPhone__c);//合同相对方联系方式

                        Map<String,Object> table = new Map<String,Object>();
                        table.put('main', new List<Object>{mainMap});
                        table.put('detail', new List<Object>{new List<Object>{detailMap}});
                        Map<String,String> data = new Map<String,String>();
                        data.put('creatorId',creatorId);
                        data.put('table', JSON.serialize( table));
                        data.put('title',title);
                        data.put('level',level);
                        data.put('workflowid',workflowId );



                      String url = Label.FWOAInterface;

               HttpRequest request1 = new HttpRequest();
              request1.setMethod('POST');
              System.debug(JSON.serialize(data));
              request1.setBody(JSON.serialize(data));
          
                request1.setEndpoint(url);
                request1.setHeader('Content-Type','text/plain');
                HttpResponse res1=  Utils.postRequest(request1,1);
                System.debug(res1.getBody());
                 if (res1.getStatusCode() == 200) {
                    String resString = res1.getBody().trim();
                    //如果是测试
                    //
                    Map<String,Object> esbresMap = new Map<String,Object>() ;

                        if(Test.isRunningTest()){
                          
                          esbresMap.put('code','0');
                          esbresMap.put('result','{\"result\":\"success\",\"data\":\"73891\",\"message\":\"流程创建成功！\"}');

                        }else{

                         esbresMap = (Map<String,Object>)JSON.deserializeUntyped(resString);

                        }

                    if(String.valueOf(esbresMap.get('code'))== '0'){

                        Map<String,Object> resMap = (Map<String,Object>)JSON.deserializeUntyped((String)esbresMap.get('result'));
                        
                        if(String.valueOf(resMap.get('result')) == 'success'){
                          
                            con.isSycToOa__c = true;
                            String oaflowID = String.valueOf(resMap.get('data'));
                            con.ProcessID__c = oaflowID;
                            if(oaflowID != null ) con.ContractURL__c = 'https://fwoa.maccura.com/workflow/request/ViewRequest.jsp?requestid='+oaflowID+'&_workflowid=1085';
                            con.Status__c = 'OA审批中';
                            update con;
                            Approval.lock(con,false);
                            return '同步成功!';

                        }else{

                            return '同步失败:'+ resMap.get('message');

                        }
                         
                                
                    }else{

                      return '同步失败:'+ esbresMap.get('msg');
                    }

                 }else{

                  return '同步失败:'+res1.getBody();
                 }

            }else{
                      return '调用OA接口报错!';

                    }

                     
                
              }else{
                  return '调用ESB接口报错!';
              }
          }else{
              return '系统错误:'+res.toString()+',请联系系统管理员!';
          }

  }
*/


  //第二步 同步并反写解约合同(解约合同id )
  //第一步 新建解约合同(主合同id )

  Webservice  static String  unTieContract(String conid ){

      String code = '0';
      String data = '';
      String msg = '';

      SavePoint sp =  Database.setSavepoint();
      try {
           List<Contract__c>  cons = [select RecordType.DeveloperName,ApproveType__c,MainContract__c,MainContract__r.ProcessID__c ,TerminalCustomer__r.Name ,

           Account__r.RecordType.DeveloperName , Status__c,ProcessID__c, ContractURL__c,CooperationType__c,Account__r.Name ,ContractSource__c,

          isSycToOa__c,Id ,Maccura__c, Maccura__r.AccountNumber,CustomerContact__r.Name , CustomerPhone__c ,Vsalestrucode__c,

          Corpcode__c,CustomerContact__c,MainContract__r.ContractNum__c 

           from Contract__c where Id =:conid];
          if(cons.isEmpty() )return '合同错误!不能发起OA流程!';
          Contract__c con = cons[0];
          //如果存在解约合同 判断是否同步
          if([select count() from Contract__c where MainContract__c =:con.Id and RecordType.DeveloperName = 'DeleteContract' ] > 0 ){

            Contract__c mcon   =  [select Id ,Status__c, isSycToOa__c from Contract__c where MainContract__c =:con.Id and RecordType.DeveloperName = 'DeleteContract' limit 1 ];
            //if(mcon.IsSycToOa__c == true ) return '已同步解约合同,请不要重复点击!';
            msg = 'success';
            data = mcon.Id;

          }else{

            Contract__c  mcon = new Contract__c();
            mcon.MainContract__c = con.Id;
            mcon.ContractSource__c = con.ContractSource__c;
            mcon.Vsalestrucode__c = con.Vsalestrucode__c;
            mcon.Corpcode__c = con.Corpcode__c;
            mcon.CooperationType__c = con.CooperationType__c;
            mcon.Account__c = con.Account__c;
            mcon.Maccura__c = con.Maccura__c;
            mcon.CustomerContact__c = con.CustomerContact__c;
            mcon.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('DeleteContract', 'Contract__c');
            mcon.ApproveType__c = '3';
            insert mcon;
            msg = 'success';
            data = mcon.Id;

          }

      }
      catch (Exception e) {
          Database.rollback(sp);
          code = '1';
          msg = 'error:'+e.getMessage();

      }
      
     
        Map<String,String> resMap = new Map<String,String>();
        resMap.put('code', code);
        resMap.put('msg', msg);
        resMap.put('data',data);
        return  JSON.serialize(resMap);   
  }


  WebService static String postToOA(String conid ){


        Map<String,String> mainMap = new Map<String,String>();
        //1.获取人员的OA id 

        Contract__c  con = [select ContractNumber__c,RecordType.DeveloperName,ApproveType__c,MainContract__c,MainContract__r.ProcessID__c ,TerminalCustomer__r.Name ,

         Account__r.RecordType.DeveloperName , Status__c,ProcessID__c, ContractURL__c,CooperationType__c,Account__r.Name ,ContractSource__c,

        isSycToOa__c,Id ,Maccura__c, Maccura__r.AccountNumber,CustomerContact__r.Name , CustomerPhone__c ,Vsalestrucode__c,Compensation__c,

        Corpcode__c,CustomerContact__c,MainContract__r.ContractNum__c ,MainContract__r.ApproveType__c,MainContract__r.ContractSource__c,
        MainContract__r.CooperationType__c,DirectRenewal__c

         from Contract__c where Id =:conid];



        if(con == null)return '合同错误!不能发起OA流程!';

        Boolean isUnContract = con.RecordType.DeveloperName == 'DeleteContract';


         Integer relatedNo = 0;

        relatedNo += [select count() from ContractProduct__c where Contract__c =:conid ];
        relatedNo += [select count() from ContractReagentInformation__c where Contract__c =:conid ];
        // relatedNo += [select count() from Measure_Calculate__c where Contract__c =:conid ];
        // relatedNo += [select count() from InstrumentPolicy__c where Contract__c =:conid ];
        // relatedNo += [select count() from BloodSugarOrder__c where Contract__c =:conid ];

        Boolean isContractChange = con.RecordType.DeveloperName == 'ChangeContract';


        Boolean isMainContract = con.RecordType.DeveloperName == 'TerminalContract';

       // 新签验证要关联合作明细就行
       // 续签那些都不用管
         if( relatedNo == 0 && isMainContract && con.ApproveType__c == '0' )return '没有合同明细!';


        Boolean isBlankMainContract  = con.MainContract__c == null ;


        if( (con.Status__c == '已生效'|| con.Status__c == '已终止')) return '合同'+con.Status__c +'!不能发起OA流程';


        if(   con.IsSycToOa__c == true) return '已同步OA不要重复同步!';

        String approveType =  con.ApproveType__c ;
        String contractSource =  isUnContract ? con.MainContract__r.ContractSource__c :con.ContractSource__c ;
        //String cooperationType = isMainContract ? con.CooperationType__c : con.MainContract__r.CooperationType__c;

        if( approveType == null  )return '合同签订类型不能为空!';

        if(contractSource == null  )return '合同来源不能为空!';

        //if(cooperationType == null  )return '合同审批类型不能为空!';

         String workflowId = isMainContract ? Label.OAContractFlowID : Label.OAContractChangeFlowId;//'3083'  // '1564' ; //'3447'

         System.debug('ContractWebService==>'+workflowId);

        if(!isMainContract && isBlankMainContract) return '主合同不能为空!';


        //if(!isMainContract && String.isBlank(con.MainContract__r.ProcessID__c)  ) return 'OA合同流程ID不能空!';
        //ChangeContract
        if(!isMainContract && con.RecordType.DeveloperName == 'ChangeContract' && con.Status__c != 'SF审批成功' && [ select count() from ContractChangeList__c where Contract__c =: con.Id ] > 0  ) return '补充变更合同必须SF审批通过才能推送 ' ;

       // https://oa.maccura.com/maccura/salesforce/htdaid.jsp?htdabh=
        String fwconId = null ;
        if(!isMainContract){

          HttpRequest conContractRequest  = new HttpRequest();
          conContractRequest.setMethod('GET');
          conContractRequest.setEndpoint(Label.FWOAGetContractId+'?htdabh='+con.MainContract__r.ContractNum__c);

          //{"id":"1365"}
          HttpResponse res=  Utils.postRequest(conContractRequest,1);
          System.debug(res.getBody());
          if(res.getStatusCode() == 200 ){

            Map<String,Object> resm = (Map<String,Object>)JSON.deserializeUntyped(res.getBody());
            fwconId = String.valueOf(resm.get('id'));
          }
          if(fwconId == null || fwconId == '')return '非法的合同编码:'+con.MainContract__r.ContractNum__c;
        }


        /* try {
              Integer.valueOf(cooperationType);
          }
          catch (Exception e) {
              
              return '合同类型异常值!';
          }*/
            

        User u = [select Id, EmployeeNumber from User  where Id =: UserInfo.getUserId()];
        if(u.EmployeeNumber == null) return '请维护合同创建人的工号!'; 

        String emNumber = null ;

        try {
           emNumber =  u.EmployeeNumber.replaceFirst('^0+', '');
        }catch (Exception e) {
            return '解析发起审批人工号错误:'+e.getMessage();
        }
         try {
            Integer.valueOf(approveType);
        }
        catch (Exception e) {
            
            return '合同签定类型异常值!';
        }
         try {
            Integer.valueOf(contractSource);
        }
        catch (Exception e) {
            
            return '合同来源异常值!';
        }
        //if(!isMainContract &&  con.Maccura__c == null) return '我方主体必填!';
        //if(con.Maccura__r.AccountNumber == null || con.Maccura__r.AccountNumber =='' )return '请维护我方主体的客户编码!';

      

       //请求Hr数据，获取系统内ID
        HttpRequest request = new HttpRequest();
        request.setMethod('GET');
        request.setEndpoint(Label.GetOAPersonCode+'?userID='+emNumber);
        HttpResponse res=  Utils.postRequest(request,2);


         if (res.getStatusCode() == 200) {
                Map<String,Object> r = new Map<String,Object>();
                //如果是测试
                if(Test.isRunningTest()){

                    Map<String,Object>  resBody= new Map<String,Object>();
                  resBody.put('code','0');
                  resBody.put('result','<UserBean-array>  <UserBean>    <userid>4854</userid>    <subcompanyid1>126</subcompanyid1>    <departmentid>3009</departmentid>    <subcompanyname>迈克电子</subcompanyname>    <departmentname>四川</departmentname>    <workcode>2896</workcode>    <lastname>文明红</lastname>    <loginid>2896</loginid>    <password>E547BF53F93AD2A0027F8F54FE28EE95</password>    <seclevel>20</seclevel>    <sex>女</sex>    <jobtitle>449</jobtitle>    <jobcall></jobcall>    <joblevel>17</joblevel>    <jobactivitydesc></jobactivitydesc>    <managerid>4848</managerid>    <assistantid></assistantid>    <status>1</status>    <locationid>21</locationid>    <workroom></workroom>    <telephone>81694</telephone>    <mobile>15882203634</mobile>    <mobilecall></mobilecall>    <fax></fax>    <email>wenminghong@maccura.com</email>    <systemlanguage></systemlanguage>    <birthday></birthday>    <folk></folk>    <nativeplace></nativeplace>    <regresidentplace></regresidentplace>    <certificatenum></certificatenum>    <maritalstatus>未婚</maritalstatus>    <policy></policy>    <bememberdate></bememberdate>    <bepartydate></bepartydate>    <islabouunion></islabouunion>    <educationlevel></educationlevel>    <degree></degree>    <healthinfo></healthinfo>    <height></height>    <weight></weight>    <residentplace></residentplace>    <homeaddress></homeaddress>    <tempresidentnumber></tempresidentnumber>    <startdate>2016-12-05</startdate>    <enddate>2900-01-01</enddate>    <createdate>2018-07-13</createdate>    <lastChangdate>2018-08-03</lastChangdate>    <accounttype>0</accounttype>    <dsporder>4983.0</dsporder>  </UserBean></UserBean-array>');
                    r =  resBody;
                }else{
                     r  = (Map<String,Object> ) JSON.deserializeUntyped(res.getBody());
                }
                 if(r.get('code') == 0 || r.get('code') == '0'){
                
                   if (r.get('result') != null ) {
                      String jsr = (String)r.get('result');
                        //格式化
                      Pattern p = Pattern.compile('\\s*|\t|\r|\n');
                      Matcher m_jsr = p.matcher(jsr);
                      String n_jsr = m_jsr.replaceAll('');
                  
                      //XML解析
                      Dom.Document doc = new Dom.Document(); 
                      doc.load(n_jsr);

                      Dom.XMLNode userDataInfo = doc.getRootElement();

                      String userid = '';
                      String subcompanyid1 = '';
                      String departmentid = '';

                      for(Dom.XMLNode node : userDataInfo.getChildren()){

                          if(node.getChildElement('status',null ).getText() < '4'){

                            userid = node.getChildElement('userid', null).getText(); 
                            subcompanyid1= node.getChildElement('subcompanyid1', null).getText();
                            departmentid = node.getChildElement('departmentid', null).getText();
                            break;
                          }
                      }

                     
                      mainMap.put('sqr',userid);//申请人;
                      mainMap.put('ssbm',departmentid);//所属部门;
                      mainMap.put('ssgs',subcompanyid1);//所属公司;
                     // mainMap.put('htmc',);//合同名称;
                    // if(subcompanyid != null ) mainMap.put('wfzt',subcompanyid);//我方主体;

                      if(isMainContract)mainMap.put('htsplx','0');//合同类型
                      if(isContractChange|| isUnContract )mainMap.put('htsplxn','0');//合同类型
                     // mainMap.put('ej',cooperationType);//二级分类(合同审批类型) 0 单一 1 打包多项目  2 三方机构集团类合同  3 三方机构非集团类合同
                      mainMap.put('zxfx','0');//直销/分销
                      mainMap.put('sfxm','0');//是否项目
                      mainMap.put('sfwdbyw','1');//是否为代开业务
                      mainMap.put('htqx','0');//合同期限
                      mainMap.put('bz','0');//币种
                      mainMap.put('hthtje','0');//合同金额
                      if(con.Compensation__c != null )mainMap.put('sfwbcj',con.Compensation__c);//是否为合同补偿金
                      
                      mainMap.put('htly',contractSource);//合同来源  0 我方全模板 1 模板修改 2 非模板 

                      mainMap.put('fkfx','0');//付款方向

                     
                      mainMap.put('qdlx',approveType );//(合同签定类型) 0 签约 1 续签 2 补充变更 3 解约

                      mainMap.put('sfbh',conid);//合同编号

                      //主合同
                    if(!isMainContract)mainMap.put('zhtbh', fwconId );

                      //合同
                      //http://192.168.42.57:82/maccura/salesforce/TokenToSF_wev.jsp?flag=a0P0I00001J7vXvUAJ
                      mainMap.put('sfhtlj','<a href=\'/maccura/salesforce/TokenToSF_wev.jsp?flag='+con.Id  +'\' target=\'__blank\' >点击查看</a>');

                        String title = '(SFNo.'+con.ContractNumber__c + ')与'+con.Account__r.Name  + '的销售合同审批';
                        String level = '0';
                        String creatorId = userid;

                        //String workflowId = Label.OAContractFlowID;//''3083'';
                      
                        Map<String,String> detailMap = new Map<String,String>();
                        detailMap.put('htxdf', con.Account__r.Name );//合同相对方
                        detailMap.put('xdflxr', con.CustomerContact__r.Name );//合同相对方联系人
                        detailMap.put('xdflxfs', con.CustomerPhone__c);//合同相对方联系方式

                        Map<String,Object> table = new Map<String,Object>();
                        table.put('main', new List<Object>{mainMap});
                      table.put('detail', new List<Object>{new List<Object>{detailMap}});
                        Map<String,String> data = new Map<String,String>();
                        data.put('creatorId',creatorId);
                        data.put('table', JSON.serialize( table));
                        data.put('title',title);
                        data.put('level',level);
                        data.put('workflowid',workflowId );



                      String url =Label.FWOAInterface;

            HttpRequest request1 = new HttpRequest();
              request1.setMethod('POST');
              System.debug(JSON.serialize(data));
              request1.setBody(JSON.serialize(data));
                // Integer i = 0 ;

                // for (String k  : data.keySet()) {
                //   url += (i == 0 ? '' : '&') +k +'='+data.get(k);
                //   i ++;
                // }
                
                request1.setEndpoint(url);
                request1.setHeader('Content-Type','text/plain');
                //request1.setHeader('Content-Type','application/x-www-form-urlencoded ; charset=utf-8');
                HttpResponse res1=  Utils.postRequest(request1,1);
                System.debug(res1.getBody());
                System.debug( '==========>');
                 if (res1.getStatusCode() == 200) {
                    String resString = res1.getBody().trim();
                    //如果是测试
                    //
                    Map<String,Object> esbresMap = new Map<String,Object>() ;

                        if(Test.isRunningTest()){
                          
                          esbresMap.put('code','0');
                          esbresMap.put('result','{\"result\":\"success\",\"data\":\"73891\",\"message\":\"流程创建成功！\"}');

                        }else{

                      esbresMap = (Map<String,Object>)JSON.deserializeUntyped(resString);

                        }

                    if(String.valueOf(esbresMap.get('code'))== '0'){

                        Map<String,Object> resMap = (Map<String,Object>)JSON.deserializeUntyped((String)esbresMap.get('result'));
                        
                        if(String.valueOf(resMap.get('result')) == 'success'){
                             String oaflowID = String.valueOf(resMap.get('data'));
                              con.isSycToOa__c = true;
                              con.ProcessID__c = oaflowID;
                              if(oaflowID != null ) con.ContractURL__c = 'https://fwoa.maccura.com/workflow/request/ViewRequest.jsp?requestid='+oaflowID+'&_workflowid='+workflowId;
                              con.Status__c= '已推送OA';
                              update con;
                              Approval.lock(con,false);
                             return '同步成功!';

                        }else{
                          return '同步失败:'+ resMap.get('message');

                        }
                         

                                
                    }else{

                      return '同步失败:'+ esbresMap.get('msg');
                    }

                 }else{

                  return '同步失败:'+res1.getBody();
                 }

                    
                   }else{
                      return '调用OA接口报错!';
                   }
                }else{
                   return '调用ESB接口报错!';
                } 
            } else {
                return '系统错误:'+res.toString()+',请联系系统管理员!';
            }
       
        
    }
/*
   //合同产品价格同步到NC 生成报价单
 public static String sycToNc(List<ConProductArgs>data){
         Map<String ,List<ConProductArgs>>temp = new Map<String ,List<ConProductArgs>>();
          temp.put('data',data);

          String body = JSON.serialize(temp);
            System.debug(body);
            HttpRequest req = new HttpRequest();
            req.setBody(body);
            //return body;
            req.setMethod('POST');
            req.setHeader('Content-Type','text/plain');
            // 接口写在自定义标签的quoteUrl里面
            req.setEndpoint(System.Label.quoteUrl);

            System.debug('+++'+ body);
            
            HttpResponse res = Utils.postRequest(req, 1);
             System.debug('+++'+res.getBody());
         
            if (res.getStatusCode() == 200) {
                Map<String,Object> r = new Map<String,Object>();
                //如果是测试
                if(Test.isRunningTest()){

                    Map<String,Object>  resBody= new Map<String,Object>();
                  resBody.put('code','0');
                  resBody.put('result','{\"result_msg\":\"操作成功！\",\"result\":1,\"data\":{\"ts\":\"2018-01-04 16:24:18\"}}');
                    r =  resBody;
                }else{
                     r  = (Map<String,Object> ) JSON.deserializeUntyped(res.getBody());
   
                }
               // return res.getBody();
                 if(r.get('code') == 0 || r.get('code') == '0'){
                  Map<String,Object> rs = (Map<String,Object>) JSON.deserializeUntyped(((String)r.get('result')));
                
                   String msg = '系统提示:';
                   msg +=  rs.get('result') == 0 ? '同步失败:' +(String) rs.get('result_msg')  :'同步成功';
                   if (rs.get('result') == 1 ) {
                      return 'OK';
                   }
                     
                  return msg;
                 }else{
                  return '系统提示:' + (String)r.get('msg') ;
                 }

                return 'OK';
            } else {
                return '系统错误:'+res.toString()+',请联系系统管理员!';
            }
   }
     global class ConProductArgs {
        global String corpcode;  //报价上新增字段 公司编码 报价.corpcode__c
        global String custcode;  //客户的NCAccountNumber__c
        global String vsalestrucode;  //报价上新增字段 销售组织编码 报价.vsalestrucode__c
        global String created_by;  //报价的创建人名字
        global String sub_corp_manager;  //报价的销售中心总监名字
        global String csourcecode;  //报价的QuoteNumber
        global String ts;  //报价的lastmodifieddate
        global String csalequotationid;  //报价的id
        global String csalequotation_bid;  //报价产品的id
        global String cemployeecode;  //报价所有者
        global String invcode;  //报价产品.产品的NCCode__c
        global String noriginalcurtaxprice;  //报价产品的SalesPrice__c
        global String nitemdiscountrate;  //报价产品的DiscountRate__c
        global String nnumber;  //报价产品的Quantity__c
        global String opertype; //操作类型   1=新增  2=修改  3=删除
        global String quotetype; //报价类型   1=报价  2=调价
        global String adjustid; //调价的id
        global String adjust_pid; //调价产品的id
        global String addCustomer; //客户终端类型
        global String currtypename ; //币种
        global String dreceiptdate;//报价日期

    }
    */
   
   /* private static String returnMsg(string reqid ){
       Map<String,String>  msg = new Map<String,String>();
     
      msg.put('-1', 'OA创建流程失败');
      msg.put('-2', 'OA没有创建权限');
      msg.put('-3', 'OA创建流程失败');
      msg.put('-4', 'OA字段或表名不正确');
      msg.put('-5', 'OA更新流程级别失败');
      msg.put('-6', 'OA无法创建流程待办任务');
      msg.put('-7', 'OA流程下一节点出错，请检查流程的配置，在OA中发起流程进行测试');
      msg.put('-8', 'OA流程节点自动赋值操作错误');

      if(msg.get(reqid) == null){

         return '同步成功!';

      }else{

        return msg.get(reqid) ;

      }
    }*/
    
}